/* 
 * This class implements a table that displays chemical names and their 
 * corresponding concentrations.
 *
 * Author: Aaron A. Lovato
 */
package gui;
 
import javax.swing.table.*;
import java.util.*;
import core.SystemController;
import core.Specie;
import core.Component;

public class ResultTableModel extends AbstractTableModel
{
    protected ResourceBundle resbundle;
    private String[] columns;
    private Object[][] data;
    
    public ResultTableModel(SystemController controller)
    {
        // The ResourceBundle below contains all of the strings used in this application. 
        // New localities can be added by adding additional properties files.
        resbundle = ResourceBundle.getBundle ("strings", Locale.getDefault());
        columns = new String[2];
        columns[0] = resbundle.getString("component");
        columns[1] = resbundle.getString("concentration");
        
        data = new Object[0][2];
        Object[] newData;
        for(Component c : controller.getComponents())
        {
            newData = new Object[2];
            newData[0] = c.getName();
            newData[1] = c.getConc();
            addRow(newData);
        }
        for(Specie s : controller.getSpecies())
        {
            newData = new Object[2];
            newData[0] = s.getName();
            newData[1] = s.getConc();
            addRow(newData);
        }
    }
    
    public int getColumnCount()
    {
        return columns.length;
    }
    
    public int getRowCount()
    {
        return data.length;
    }
    
    public String getColumnName(int col) 
    {
        return columns[col];
    }

    public Object getValueAt(int row, int col) 
    {
        return data[row][col];
    }
    
    public Class getColumnClass(int c) 
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) 
    {
        return false;
    }

    public void setValueAt(Object value, int row, int col) 
    {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
    
    public void addRow(Object[] row)
    {
        int newLength = data.length + 1;
        Object[][] newData = new Object[newLength][2];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
        data[newLength-1][0] = row[0];
        data[newLength-1][1] = row[1];

        fireTableRowsInserted(data.length, data.length);
    }
}
